import axios from "axios"
import qs from "qs"
import md5 from "blueimp-md5"
import { getCookie, setCookie } from "../utils/cookies"

function urlEncode(param: any, key?: any, encode?: any) {
  if (param == null) return ""
  var paramStr = ""
  var t = typeof param
  if (t === "string" || t === "number" || t === "boolean") {
    paramStr += "&" + key + "=" + (encode == null || encode ? encodeURIComponent(param) : param)
  } else {
    for (var i in param) {
      var k = key == null ? i : key + (param instanceof Array ? "[" + i + "]" : "." + i)
      paramStr += urlEncode(param[i], k, encode)
    }
  }
  return paramStr
}

const timeout = 10000

// const ballBetUrl = "https://www.tozuuh.com"
// const ballBetUrl = "https://ballbet.com"
// const ballBetUrl = "https://www.tozuuh.com"
// const ballBetUrl = "https://www.okzpps.com"
// const ballBetUrl = "https://www.uubody.com"

const instanceBallbet = axios.create({
  baseURL: "",
  timeout: timeout,
  headers: {
    "Content-type": "application/x-www-form-urlencoded",
    Accept: "application/json, text/plain, */*",
    version: "1.0.2",
    "device-id": "1",
    "os-type": "2",
    "app-type": 0
  },
  withCredentials: true
})

const instanceAtypia = axios.create({
  baseURL: "https://pxdd.tozuuh.com",
  timeout: timeout
})

// const xjBase = "http://xj-sb-asia-mrball.prdasbbwla1.com"

const instanceXJBase = axios.create({
  baseURL: "",
  headers: {
    "Content-type": "application/json",
    Referer: "http://www.aibet.com/zh-cn/sportsbook"
  },
  withCredentials: true
})

if (getCookie("token")) {
  instanceBallbet.defaults.headers["token"] = getCookie("token")
}

if (getCookie("uid")) {
  instanceBallbet.defaults.headers["token"] = getCookie("uid")
}

const http = {
  post(repo: string, param?: object) {
    this.setSign()
    return new Promise((resolve, reject) => {
      instanceBallbet
        .post(repo, qs.stringify(param))
        .then(response => {
          resolve(response.data)
        })
        .catch(error => {
          return reject(error)
        })
    })
  },

  psotAtypia(repo: string, param?: object) {
    return new Promise((resolve, reject) => {
      instanceAtypia
        .post(repo, qs.stringify(param))
        .then(response => {
          resolve(response)
        })
        .catch(error => {
          return reject(error)
        })
    })
  },

  getXJ(repo: string) {
    return new Promise((resolve, reject) => {
      instanceXJBase
        .get(repo)
        .then(response => {
          resolve(response.data)
        })
        .catch(error => {
          return reject(error)
        })
    })
  },

  postXJParam(repo: string, param?: object) {
    return new Promise((resolve, reject) => {
      instanceXJBase
        .post(repo, param)
        .then(response => {
          resolve(response.data)
        })
        .catch(error => {
          return reject(error)
        })
    })
  },

  setAuth(token: string, uid: string, username: string, remberPsw: boolean, password: number) {
    instanceBallbet.defaults.headers["token"] = token
    instanceBallbet.defaults.headers["uid"] = uid
    setCookie("token", token, 10000)
    setCookie("uid", uid.toString(), 10000)
  },

  /**
   * 小金域名限制，测试环境开启
   */
  setXJProxy() {
    instanceXJBase.defaults.headers["x-forwarded-for"] = "27.109.32.0"
  },

  setSign() {
    const signParams: any = {}
    signParams["device-id"] = "1"
    signParams["os-type"] = "2"
    signParams["timestamp"] = Date.now() + ""
    signParams["version"] = "1.0.2"

    instanceBallbet.defaults.headers["timestamp"] = signParams["timestamp"]
    const urlStr = urlEncode(signParams).substr(1)
    instanceBallbet.defaults.headers["sign"] = md5(urlStr + "global")
  }
}
export default http
